package com.zw.pdm.module.file.service;

import com.zw.pdm.constant.Constant;
import com.zw.pdm.entity.production.FileSheet;
import com.zw.pdm.entity.rep.VerifyProcess;
import com.zw.pdm.module.file.mapper.FileConfirmMapper;
import com.zw.pdm.module.file.repository.FileSheetRepository;
import com.zw.pdm.module.report.mapper.VerifyProcessMapper;
import com.zw.pdm.module.report.repository.VerifyProcessRepository;
import com.zw.pdm.module.system.service.LogService;
import com.zw.pdm.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static com.zw.pdm.util.BeanUtil.copy;

/**
 * @author mayongfeng
 * @version 1.0.0
 * @ClassName FileConfirmService.java
 * @Description TODO
 * @createTime 2020年12月16日 10:58:00
 */
@Service
@SuppressWarnings("all")
public class FileConfirmService {
    @Autowired
    private FileConfirmMapper fileConfirmMapper;
    @Autowired
    private FileSheetRepository fileSheetRepository;
    @Autowired
    private LogService logService;
    @Autowired
    private VerifyProcessMapper verifyProcessMapper;
    @Autowired
    private VerifyProcessRepository verifyProcessRepository;
    @Value("${saleUrl}")
    private String saleUrl;

    /**
     * @Author mayongfeng
     * @Date 10:59 2020/12/16
     * @Description 查询审核过后的存档列表
     *
     **/
    public List<FileSheet> findFileConfirm(FileSheet fileSheet) {
       return fileConfirmMapper.findFileConfirm(fileSheet);

    }

    /** 下发到档案室*/
    public FileSheet confirm(Integer id) {
        FileSheet fileSheet = fileSheetRepository.getOne(id);
        /** 下发到档案室状态designStatus 0=未下发，1=已下发*/
        fileSheet.setDesignStatus(1);
        fileSheet.setDesiSigner(ShiroUtils.getUser().getName());
        fileSheet.setVerifyTime(new Date());
        logService.saveLog("下发存档", "存档ID：" + id);
        return fileSheetRepository.save(fileSheet);
    }

    /** 驳回存档清单*/
    public int fileReject(FileSheet sheet) {
        VerifyProcess vp = verifyProcessMapper.findCurVpByFileConfirmId(sheet.getId());
        logService.saveLog("驳回存档清单Id", "存单清单ID：" + vp.getId());
        FileSheet fileSheet = fileSheetRepository.getOne(sheet.getId());
        copy(sheet, fileSheet);
        fileSheet.setStatus(Constant.STATUS_COMMIT);
        fileSheetRepository.saveAndFlush(fileSheet);
        vp.setStatus(0);
        vp.setCur(1);
        vp.setVerifyTime(new Date());
        verifyProcessRepository.saveAndFlush(vp);
        return 1;
    }

    /** 完结列表 */
    public List<FileSheet> findEndList(FileSheet fileSheet) {
        return fileConfirmMapper.findEndList(fileSheet);
    }

    /** 按部分查询*/
    public List<FileSheet> findBrancHSheet(FileSheet fileSheet) {
        return  fileConfirmMapper.findBrancHSheet(fileSheet);
    }

    /** 项目已完结*/
    public boolean updateEndStatusById(Integer id) {
        FileSheet fileSheet = fileSheetRepository.getOne(id);
        fileSheet.setEndStatus(1);
        fileSheet.setEndTime(new Date());
        fileSheetRepository.saveAndFlush(fileSheet);
        String a= saleUrl;
        boolean boolFlg = updateProSaveStatus(saleUrl + "/projectScheduling/updateSaveStatus",fileSheet.getProCode());
        if (!boolFlg) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }

    /**
     * 跨域修改 项目状态
     *
     * @param saleUrl   接口地址
     * @param proCode   项目编号
     * @param SaveStatus 项目状态
     * @return
     */
    private boolean updateProSaveStatus(String saleUrl, String proCode) {
        RestTemplate restTemplate = new RestTemplate();
        MultiValueMap<String, String> bodyMap = new LinkedMultiValueMap<>();
        bodyMap.add("proCode", proCode);
        String completeTime = "";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date = new Date();
        completeTime = simpleDateFormat.format(date);
        bodyMap.add("completeTime",completeTime);
        return restTemplate.postForObject(saleUrl, bodyMap, Boolean.class);
    }

   /** 编辑档案备注*/
    public FileSheet modifyFile(FileSheet fileSheet) {
        FileSheet fileSheet1 = fileSheetRepository.getOne(fileSheet.getId());
        fileSheet1.setFilePosition(fileSheet.getFilePosition());
        return fileSheetRepository.save(fileSheet1);
    }
}
